package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.AddressAddBean;
import com.yiwei.zxp.api.bean.SmsSendBean;
import com.yiwei.zxp.api.model.AddressListsModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.event.AddAddressEvent;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.view.SelectAreaDilaog;
import com.yiwei.zxp.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class AddressEditActivity extends BaseActivity implements SelectAreaDilaog.OnChooseListener {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_adress)
    TextView editAdress;
    @BindView(R.id.edit_area)
    EditText editArea;
    @BindView(R.id.check_default)
    ImageView checkDefault;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Unbinder unbiner;
    private int isDefault = 0;
    private SelectAreaDilaog selectAreaDialog;
    private AddressAddBean submitData;
    private AddressListsModel.DatasBean addressdata;
    private boolean isSelectAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_edit);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        submitData = new AddressAddBean();
        isSelectAddress = getIntent().getBooleanExtra("is_select", false);
        addressdata = getIntent().getParcelableExtra("data");
        if (addressdata != null) {
            editName.setText(addressdata.name);
            editPhone.setText(addressdata.phone);
            editAdress.setText(addressdata.headAddress);
            editArea.setText(addressdata.address);
            if (addressdata.is_default == 1) {
                isDefault = 1;
                checkDefault.setImageResource(R.mipmap.img_turn_on);
            } else {
                isDefault = 0;
                checkDefault.setImageResource(R.mipmap.img_turn_off);
            }


            submitData.province_id = addressdata.province_id;
            submitData.city_id = addressdata.city_id;
            submitData.district_id = addressdata.district_id;
            submitData.id = addressdata.id;
        }

        txtTitle.setText("填写地址");
        selectAreaDialog = new SelectAreaDilaog(this).setOnChooseListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnTextChanged({R.id.edit_phone, R.id.edit_name, R.id.edit_area})
    void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(UI.toString(editPhone)) || TextUtils.isEmpty(UI.toString(editName)) || TextUtils.isEmpty(UI.toString(editAdress)) || TextUtils.isEmpty(UI.toString(editArea))) {
            btnSubmit.setSelected(false);
        } else {
            if (UI.toString(editPhone).length() == 11) {
                btnSubmit.setSelected(true);
            } else {
                btnSubmit.setSelected(false);
            }
        }
    }

    public void getAddressAdd(AddressAddBean item) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        Api.api_service.getAddressAdd(item).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (TextUtils.isEmpty(item.id)) {
                        ToastUtil.makeText(this, "添加成功");
                    } else {
                        ToastUtil.makeText(this, "修改成功");
                    }
                    if (isSelectAddress) {
                        EventBus.getDefault().post(new AddAddressEvent(item, UI.toString(editAdress), UI.toString(editArea)));
                    }
                    finish();
                });
    }

    @OnClick({R.id.btn_back, R.id.edit_adress, R.id.check_default, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.edit_adress:
                selectAreaDialog.show();
                break;
            case R.id.check_default:
                if (isDefault == 1) {
                    isDefault = 0;
                    checkDefault.setImageResource(R.mipmap.img_turn_off);
                } else {
                    isDefault = 1;
                    checkDefault.setImageResource(R.mipmap.img_turn_on);
                }
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editName))) {
                    ToastUtil.makeText(this, "收货人不能为空");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editPhone))) {
                    ToastUtil.makeText(this, "联系电话不能为空");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editAdress))) {
                    ToastUtil.makeText(this, "所在地区不能为空");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editArea))) {
                    ToastUtil.makeText(this, "详细地址不能为空");
                    return;
                }
                submitData.address = UI.toString(editArea);
                submitData.name = UI.toString(editName);
                submitData.phone = UI.toString(editPhone);
                submitData.is_default = isDefault + "";
                getAddressAdd(submitData);
                break;
        }
    }

    @Override
    public void onAddressSelect(String provinceId, String provinceName, String cityId, String cityName, String areaId, String areaName) {
        submitData.province_id = provinceId;
        submitData.city_id = cityId;
        submitData.district_id = areaId;
        editAdress.setText(provinceName + cityName + areaName);
    }
}
